package cn.itlaobing.web.servlets;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.http.HTTPException;

import cn.itlaobing.businessexception.BusinessException;
import cn.itlaobing.model.GoodsModel;
import cn.itlaobing.model.UserModel;
import cn.itlaobing.service.UserService;
import cn.itlaobing.service.impl.UserServiceImpl;
import cn.itlaobing.web.Constants;
import cn.itlaobing.web.annotation.RequestMapping;
/**
 * 实现登录的servlet、
 * */
@WebServlet(value = "/user/*", loadOnStartup = 1)
public class UserServlet extends BaseServlet {
	@RequestMapping("/login")
	public void login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		request.setCharacterEncoding("utf-8");
		String user_name = request.getParameter("user_name");
		String user_pwd = request.getParameter("user_pwd");
		UserService us=new UserServiceImpl();
		UserModel um=us.authentication(user_name,user_pwd);
		if(um==null){
			request.setAttribute("msg","账户或密码错误");
			request.getRequestDispatcher("/login.jsp").forward(request,response);
			return;
		}
		//登录成功，将数据存入session中，使用重定向
		request.getSession().setAttribute(Constants.CURRENT_USER,um);
		response.sendRedirect(request.getContextPath()+"/goods/show");
	}
}
